package com.atguigu.multiReading;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

class MyThreadPool{
    private BlockingQueue<Runnable> queue=new LinkedBlockingQueue<>();
    public MyThreadPool(int m){
        //在构造方法中创建出M个线程，负责完成工作
        for(int i=0;i<m;i++){
            Thread t=new Thread(()->{
                while(true){
                    try {
                        Runnable runnable=queue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();
        }
    }

    public void submit(Runnable runnable){
        try {
            queue.put(runnable);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public class demo6 {
    public static void main(String[] args) {
        MyThreadPool myThreadPool=new MyThreadPool(10);
        for(int i=0;i<1000;i++){
            int cur=i;
            myThreadPool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("执行当前任务："+cur+"  当前线程为:"+Thread.currentThread().getName());
                }
            });
        }
    }
}
